package com.songlanyun.modules.account.controller.api;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.account.entity.AccountEntity;
import com.songlanyun.modules.account.model.dto.UpdateUserInfoDTO;
import com.songlanyun.modules.exception.MemberException;
import com.songlanyun.modules.wechat.config.WxMaConfiguration;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.account.entity.UserInfoEntity;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.common.utils.R;


/**
 * 会员的信息表（如昵称，头像等）
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-06 14:07:42
 */
@RestController
@RequestMapping("api/account/userinfo")
public class UserInfoApiController extends AbstractApiController {
    @Autowired
    private UserInfoService userInfoService;


    /**
     * 信息
     */
    @GetMapping("/info")
//    @RequiresPermissions("account:yxtuserinfo:info")
    public R info() {
        UserInfoEntity yxtUserInfo = userInfoService.getById(getUserId(false));

        return R.ok().put("data", yxtUserInfo);
    }

    /**
     * 保存会员的个人信息
     */
    @PostMapping("/saveWxInfo")
//    @RequiresPermissions("account:yxtuserinfo:save")
    public R save(@RequestBody UserInfoEntity userInfoEntity) {
        Long userId = getUserId(true);
        UserInfoEntity userInfo = userInfoService.getById(userId);

        if (userInfo == null) {
            userInfoEntity.setId(userId);
            userInfoService.save(userInfoEntity);
        } else if (StringUtils.isBlank(userInfo.getNickname())) {
            BeanUtil.copyPropertiesIgnoreNull(userInfoEntity, userInfo);
            userInfoService.updateById(userInfo);
        }

        return R.ok();

    }

    /**
     * APP - 编辑资料
     */
    @PutMapping("/updateInfo")
//    @RequiresPermissions("account:yxtuserinfo:save")
    public R update(@RequestBody UpdateUserInfoDTO dto) {
        ValidatorUtils.validateEntity(dto);
        userInfoService.updateInfo(getUserId(true), dto);

        return R.ok();
    }

    /**
     * APP - 修改个人简介
     */
    @PutMapping("/update-resume")
//    @RequiresPermissions("account:yxtuserinfo:save")
    public R updateResume(@RequestBody UpdateUserInfoDTO dto) {
        if (dto.getResume() == null) {
            throw new RRException(MemberException.RESUME_NOT_EXIST);
        }
        userInfoService.updateInfo(getUserId(true), dto);

        return R.ok();
    }
}
